import { Tool, ToolColor } from '../../../Tool';

/**
 * 坐标测量
 */
export class LocationTool extends Tool {
	constructor(viewer, options = {}, ...args) {
		super(arguments);
		this.viewer = viewer;
		this.options = options;
		this.float = options?.float ?? 2;

		this.dataSource = new Cesium.CustomDataSource('fe-location');
		this.viewer.dataSources.add(this.dataSource);
		this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
		viewer.scene.layers.layerQueue.forEach((item) => {
			item.selectEnabled = false;
			item.releaseSelection();
		});
		[...arguments].forEach((item) => {
			if (typeof item === 'function') {
				this.callback = item;
			}
		});
		this.callbackValue = {
			total: null,
			current: null,
			tool: this
		};
	}

	start() {
		const labelEntity = this.dataSource.entities.add({
			label: { show: false }
		});
		const toDegrees = (val) => (val * 180) / Math.PI;

		this.handler.setInputAction((movement) => {
			var cartesian = this.viewer.scene.pickPosition(movement.endPosition);
			if (Cesium.defined(cartesian)) {
				const cartographic = Cesium.Cartographic.fromCartesian(cartesian);

				const latDir = cartographic.latitude < 0 ? 'S' : 'N';
				const longDir = cartographic.longitude < 0 ? 'W' : 'E';
				const lon = `${toDegrees(Math.abs(cartographic.longitude)).toFixed(this.float)}${longDir}`;
				const lat = `${toDegrees(Math.abs(cartographic.latitude)).toFixed(this.float)}${latDir}`;
				const height = cartographic.height.toFixed(this.float) + 'm';

				const X = cartesian.x.toFixed(this.float);
				const Y = cartesian.y.toFixed(this.float);
				const Z = cartesian.z.toFixed(this.float);

				labelEntity.position = cartesian;
				labelEntity.label.show = true;
				labelEntity.label.text = `X: ${X}\nY: ${Y}\nZ: ${Z}\n经度: ${lon}\n纬度: ${lat}\n海拔: ${height} `;
				labelEntity.label.font = '12px sans-serif';
				labelEntity.label.showBackground = true;
				labelEntity.label.backgroundColor = Cesium.Color.fromCssColorString(
					ToolColor.toRgba(ToolColor.backgroundColor, 0.8)
				);
				labelEntity.label.fillColor = Cesium.Color.fromCssColorString(ToolColor.color);
				labelEntity.label.style = Cesium.LabelStyle.FILL;

				labelEntity.label.disableDepthTestDistance = Number.POSITIVE_INFINITY;
				labelEntity.label.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
				labelEntity.label.horizontalOrigin = Cesium.HorizontalOrigin.LEFT;
				labelEntity.label.pixelOffset = new Cesium.Cartesian2(12, 24);
			} else {
				labelEntity.label.show = false;
			}
		}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

		this.handler.setInputAction((e) => {
			const cartesian = this.viewer.scene.pickPosition(e.position);
			const cartographic = Cesium.Cartographic.fromCartesian(cartesian);

			const latDir = cartographic.latitude < 0 ? 'S' : 'N';
			const longDir = cartographic.longitude < 0 ? 'W' : 'E';
			const lon = `${toDegrees(Math.abs(cartographic.longitude)).toFixed(this.float)}${longDir}`;
			const lat = `${toDegrees(Math.abs(cartographic.latitude)).toFixed(this.float)}${latDir}`;
			const height = cartographic.height.toFixed(this.float) + 'm';

			const X = cartesian.x.toFixed(this.float);
			const Y = cartesian.y.toFixed(this.float);
			const Z = cartesian.z.toFixed(this.float);

			this.dataSource.entities.add({
				position: cartesian,
				point: {
					color: Cesium.Color.WHITE,
					pixelSize: 2,
					outlineColor: Cesium.Color.fromCssColorString(ToolColor.functionalColor),
					outlineWidth: 2,
					disableDepthTestDistance: Number.POSITIVE_INFINITY
				},
				label: {
					text: `X: ${X}\nY: ${Y}\nZ: ${Z}\n经度: ${lon}\n纬度: ${lat}\n海拔: ${height} `,
					font: '12px sans-serif',
					showBackground: true,
					backgroundColor: Cesium.Color.fromCssColorString(
						ToolColor.toRgba(ToolColor.backgroundColor, 0.8)
					),
					fillColor: Cesium.Color.fromCssColorString(ToolColor.color),
					disableDepthTestDistance: Number.POSITIVE_INFINITY,
					verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
					horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
					pixelOffset: new Cesium.Cartesian2(12, 24)
				}
			});

			this.callbackValue.current = { x: X, y: Y, z: Z, lon, lat, height };
			this.callback && this.callback(this.callbackValue);
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}

	cancel() {
		this.handler.destroy();
		this.dataSource.entities.removeAll();
		this.viewer.dataSources.remove(this.dataSource);
	}
}
LocationTool.toolId = 'LocationTool';
